package Function;

import VWorldGameStudio.FCBattleEventOrder;
import VWorldGameStudio.FCEvent;
import VWorldGameStudio.events.*;
import com.google.gson.Gson;
import lombok.Getter;
import utils.FCDataSingles;

import java.io.File;
import java.util.ArrayList;
import java.util.Vector;

public class FCEventLibrary {
    public ArrayList<String> allEventTypes = new ArrayList<>();

    // 地图跳转事件信息
    private ArrayList<FCEvent> entitylist = new ArrayList<FCEvent>();
    @Getter
    private ArrayList<String> entityname = new ArrayList<String>();
    private static final String EVENT_LIST_PATH = "./data/eventlist.vw";
    private static final String EVENTS_PATH = "./data/event";
    private int eventallnum = 0;

    /**
     * 初始化事件类，读取数量
     * 建议游戏启动方使用
     *
     * @date 2021-11-30
     */
    public FCEventLibrary() {
        allEventTypes.add("jump");// 地图跳转
        allEventTypes.add("shop");// 商店
        allEventTypes.add("case");// 箱子
        allEventTypes.add("say");// npc对话
        allEventTypes.add("phonograph");// 留声机
        allEventTypes.add("door");//门事件
        allEventTypes.add("on-off");// 比如门自动开关检测
        allEventTypes.add("battle");//战斗触发
        allEventTypes.add("itemCtrl");//物品控制-在对话中控制
        allEventTypes.add("stepOn");//表示踩上去就自动触发的事件处理
        allEventTypes.add("openChuanzhen");//打开传真,没内容标志用
        allEventTypes.add("repairService");//修理服务,没内容标志用
        allEventTypes.add("fillService");//补满服务,没内容标志用
        allEventTypes.add("retire");//退休,没内容标志用
        allEventTypes.add("anMo");//按摩,没内容标志用
        allEventTypes.add("touzi");//
        allEventTypes.add("saveAndTake");//保管物品的,没内容标志用
        allEventTypes.add("upgradeCdevice");//C装置改造类型,没内容标志用
        allEventTypes.add("upgradeDiPan");//底盘改造类型,没内容标志用
        allEventTypes.add("upgradeEngine");//引擎升级,没内容标志用
        allEventTypes.add("xiCheShop");//洗车店,没内容标志用
        allEventTypes.add("mapHiddenItem");//地图宝藏
        allEventTypes.add("monsterTest");//测试怪物战斗的

        try {
            this.eventallnum = new File(EVENTS_PATH).listFiles().length;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 事件数据加载函数
     *
     * @date 2021-11-30
     */
    public void loadEventData() {
        try {
            Gson gson = new Gson();
            File[] f = new File(EVENTS_PATH).listFiles();
            for (int i = 0; i < eventallnum; i++) {
                FCEvent event = FCDataSingles.getReader().readEventData(f[i].getPath());
                entitylist.add(event);
                entityname.add(event.getName());

                System.out.println("事件--" + gson.toJson(event));
            }

//            for (int i = 0; i < eventallnum; i++) {
//                FCEvent event = entitylist.get(i);
//                registerEntity(true, 0, event.getName(), event.getType(),event.getDestination(),
//                        event.getJumpx(), event.getJumpy(),event.getMusicnum0(), event.getMusicnum1(),
//                        event.isIsvending(), event.getItemid(), event.getItemprice(), event.getRewarditemid(),
//                        event.getCasemoney(), event.getCaseitem()[0], event.getCaseitem()[1], event.getCaseitem()[2],
//                        event.getCaseitem()[3],event.getSaytext(), event.getPhonographname(),event.getPhonographnum(),
//                        event.getEventOrders(), event.getMonsterId(), event.getContainSayTextId(), event.getContainCommand(),
//                        event.getContainOtherEventId(), event.getType2());
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取事件实际数据量
     *
     * @return integer
     * @date 2021-11-30
     */
    public int getEventAllNum() {
        return this.eventallnum;
    }


    public void registerEntity(String name, String type, String destination, int jumpx,
                               int jumpy, int musicnum0, int musicnum1, boolean isvending, Vector<String> itemid,
                               Vector<Integer> itemprice, String rerwarditemid, long casemoney, String caseitem1, String caseitem2,
                               String caseitem3, String caseitem4, Vector<String> saytext, Vector<String> phname, Vector<Integer> phnum
            , ArrayList<FCBattleEventOrder> eventOrders, ArrayList<String> monsters, String containSayId,
                               String stepOnCmds, String containEventId, String type2, String miaoShu) {
        FCEvent en = new FCEvent(type, name);
        if ("jump".equals(type)) {
            JumpEvent j = new JumpEvent(type, name);
            j.setDestination(destination);
            j.setJumpx(jumpx);
            j.setJumpy(jumpy);
            j.setMusicnum0(musicnum0);
            j.setMusicnum1(musicnum1);
            en = j;
        } else if ("shop".equals(type)) {
            ShopEvent shopEvent = new ShopEvent(type, name);
            shopEvent.setIsvending(isvending);
            shopEvent.setType2(type2);
            shopEvent.setRewarditemid(rerwarditemid);
            shopEvent.getItemid().addAll(itemid);
            shopEvent.getItemprice().addAll(itemprice);
            en = shopEvent;
        } else if ("case".equals(type)) {
            CaseEvent caseEvent = new CaseEvent(type, name);
            caseEvent.setCasemoney(casemoney);
            caseEvent.caseitem[0] = caseitem1;
            caseEvent.caseitem[1] = caseitem2;
            caseEvent.caseitem[2] = caseitem3;
            caseEvent.caseitem[3] = caseitem4;
            en = caseEvent;
        } else if ("say".equals(type)) {
            SayEvent sayEvent = new SayEvent(type, name);
            sayEvent.addSayText(saytext);
            en = sayEvent;
        } else if ("phonograph".equals(type)) {
            PhonographEvent phonographEvent = new PhonographEvent(type, name);
            phonographEvent.getPhonographnum().addAll(phnum);
            phonographEvent.getPhonographname().addAll(phname);
            en = phonographEvent;
        } else if ("on-off".equals(type)) {
            OnOffEvent onOffEvent = new OnOffEvent(type, name);
            //onOffEvent.setPwd 有密码的
            en = onOffEvent;
        } else if ("battle".equals(type)) {
            BattleEvent battleEvent = new BattleEvent(type, name);
            battleEvent.setContainSayTextId(containSayId);
            battleEvent.getEventOrders().addAll(eventOrders);
            battleEvent.getMonsterId().addAll(monsters);
            en = battleEvent;
        } else if ("itemCtrl".equals(type)) {
            ItemCtrlEvent ctrlEvent = new ItemCtrlEvent(type, name);
            ctrlEvent.setContainSayTextId(containSayId);
            en = ctrlEvent;
        } else if ("stepOn".equals(type)) {
            StepOnEvent stepOnEvent = new StepOnEvent(type, name);
            stepOnEvent.setContainOtherEventId(containEventId);
            stepOnEvent.setContainCommand(stepOnCmds);
            en = stepOnEvent;
        } else if ("mapHiddenItem".equals(type)) {
            MapHiddenEvent hiddenEvent = new MapHiddenEvent(type, name);
            hiddenEvent.setMoney((int) casemoney);
            hiddenEvent.setHiddenitemId(caseitem1);
            en = hiddenEvent;
        } else if ("door".equals(type)) {
            DoorEvent doorEvent = new DoorEvent(type, name);
            doorEvent.setNeedItemKey(caseitem1);
            en = doorEvent;
        }
        en.setDescribe(miaoShu);
        if (entityname.contains(name)) {
            entitylist.remove(entityname.indexOf(name));
            entityname.remove(name);
        }

        entitylist.add(en);
        entityname.add(name);

        FCDataSingles.getWriter().writeEventData(en);
    }

    public FCEvent getEventOfName(String name) {
        if (name == null) return null;
        if (name.equals("null")) return null;
        int num = entityname.indexOf(name);
        if (num > -1)
            return entitylist.get(num);
        return null;
    }

    public ArrayList<String> getAllSayEvent() {
        return getEventsByType("say");
    }

    public int getEventNum() {
        return entityname.size();
    }

    //过滤以下能放到地图上的事件，比如门事件，踩事件，jump事件
    public ArrayList<String> getEditInMapEvents() {
        ArrayList<String> ret = new ArrayList<>();
        for (FCEvent event : entitylist) {
            if (event.getType().equals("jump") ||event.getType().equals("case") ||event.getType().equals("door")
            ||event.getType().equals("mapHiddenItem")||event.getType().equals("stepOn")||event.getType().equals("itemCtrl")
                    ||event.getType().equals("on-off")||event.getType().equals("phonograph")
                    ||event.getType().equals("openChuanzhen")) {
                ret.add(event.getName());
            }else if(event.getType().equals("shop")){
                ShopEvent shopEvent = (ShopEvent) event;
                //自动售卖机
                if(shopEvent.getType2().equals("def") && shopEvent.isIsvending()){
                    ret.add(event.getName());
                }
            }
        }
        return ret;
    }

    public ArrayList<String> getEventsByType(String type) {
        ArrayList<String> ids = new ArrayList<>();
        for (FCEvent event : entitylist) {
            if (event.getType().equals(type)) {
                ids.add(event.getName());
            }
        }
        return ids;
    }

    public String getEventNameOfID(int id) {
        return entityname.get(id);
    }

    public String getEventInfoOfID(int id) {
        FCEvent e = entitylist.get(id);
        return e.toString();
    }

    public void setCaseInfo(String id, String item) {
        int as = entityname.indexOf(id);
        if (as > -1) {
            CaseEvent qw = (CaseEvent) entitylist.get(as);
            qw.setCaseMoney();
            qw.setCaseitem(item.split(","));
            entitylist.remove(as);
            entityname.remove(as);
            entitylist.add(qw);
            entityname.add(id);
        }
    }

    public int getMusicNumOfMapName(String name) {
        for (int i = 0; i < entitylist.size(); i++) {
            if (entitylist.get(i).getType().equals("jump") && ((JumpEvent) entitylist.get(i)).getDestination().equals(name))
                return ((JumpEvent) entitylist.get(i)).getMusicnum1();
        }
        return 4;
    }

}
